package xrz.com.jr.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;

import xrz.com.jr.R;

public class BaseDialog {

    private static Dialog dialog;

    private static void createDialog(Context ctx) {
        if (dialog == null) {
            dialog = new Dialog(ctx, R.style.style_dialog);
            dialog.setCanceledOnTouchOutside(false);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.CENTER); // �˴���������dialog��ʾ��λ��
        }
    }

    public static void dismiss() {
        dialog.dismiss();
        dialog = null;
    }

    /**
     * �����Զ���Ի���
     *
     * @param ctx ������
     */
    public static void showDialog(Context ctx, final onDialogBtnClickListener listener) {
        createDialog(ctx);
        View view = LayoutInflater.from(ctx).inflate(R.layout.layout_dialog,
                null);
        Button Btn_Submit = (Button) view.findViewById(R.id.btn_submit);
        Button Btn_CanCle = (Button) view.findViewById(R.id.btn_cancle);
        Btn_CanCle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        Btn_Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                listener.onClick();
            }
        });
        int width = ScreenUtil.getInstant().getDisWidth(ctx);
        LayoutParams layoutParams = new LayoutParams(width - 60,
                LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, layoutParams);
        dialog.show();
    }

    public interface onDialogBtnClickListener {
        void onClick();
    }
}
